package br.com.fogas.medicao.activity.helper;

import br.com.fogas.database.FogasDatabase;
import br.com.fogas.medicao.R;
import br.com.fogas.medicao.activity.SincronizacaoActivity;
import br.com.fogas.medicao.activity.validation.LoginValidacao;
import br.com.fogas.medicao.application.Constants;
import br.com.fogas.utils.FogasUtility;
import br.com.fogas.utils.dialog.DialogAlert;
import br.com.fogas.utils.dialog.DialogAlertConfirm;
import br.com.fogas.utils.dialog.DialogAlertConfirmListener;
import br.com.fogas.webservice.service.FogasWebservice;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import android.content.Intent;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.widget.Button;
import android.widget.EditText;

public class LoginHelper {
	
	private FogasDatabase fogasDatabase;

	private FogasWebservice fogasWebservice;
	
	private FragmentActivity activity;

	@InjectView(R.id.btnObterImei) Button btnObterImei;

	@InjectView(R.id.btnEntrar) Button btnEntrar;

	@InjectView(R.id.etToken) EditText etToken;

	public LoginHelper(FragmentActivity activity) {
		this.activity = activity;
		this.fogasDatabase = new FogasDatabase(activity);
		this.fogasWebservice = new FogasWebservice(activity);
		ButterKnife.inject(this, activity);
	}
	
	@OnClick(R.id.btnEntrar) 
	public void entrar(Button button) {
		if(LoginValidacao.validar(this)) {
			dialog();
		}
	}
	
	@OnClick(R.id.btnObterImei) 
	public void obterImei(Button button) {
		String imei = FogasUtility.android.getIMEI(button.getContext());
		DialogAlert.newInstance()
		.setTitle(R.string.label_login_alert_imei)
		.setCharMessage(imei)
		.showDialog(activity.getSupportFragmentManager());
	}
	
	DialogAlertConfirmListener confirmListener = new DialogAlertConfirmListener() {
		
		@Override
		public void onPositiveClick(DialogFragment dialog) {
			dialog.dismiss();
			login();
		}
		
		@Override
		public void onNegativeClick(DialogFragment dialog) {
			dialog.dismiss();
		}
	};
	
	private void dialog() {
		if(FogasUtility.android.isConnectedMobile(activity)) {
			DialogAlertConfirm.newInstance(confirmListener)
			.setTitle(R.string.dialog_title_alert)
			.setMessage(R.string.dialog_msg_login_conectadointernetmovel)
			.setPositiveButtonId(R.string.sim)
			.setNegativeButtonId(R.string.nao)
			.showDialog(activity.getSupportFragmentManager());
		} else {
			login();
		} 
	}
	
	private void login() {
		String token = etToken.getText().toString().trim();
		String imei = FogasUtility.android.getIMEI(activity);
		Intent intent = new Intent(activity, SincronizacaoActivity.class);
		intent.putExtra(Constants.TOKEN, token);
		intent.putExtra(Constants.IMEI, imei);
		activity.startActivity(intent);
		activity.finish();
	}
	
	public FragmentActivity getActivity() {
		return activity;
	}

	public Button getBtnEntrar() {
		return btnEntrar;
	}

	public Button getBtnObterImei() {
		return btnObterImei;
	}
	
	public EditText getEtToken() {
		return etToken;
	}
	
	public FogasDatabase getFogasDatabase() {
		return fogasDatabase;
	}
	
	public FogasWebservice getFogasWebservice() {
		return fogasWebservice;
	}
}